package wassalt.client;

import static wassalt.Utils.AppConstants.HTTP_RESPONSE;
import static wassalt.Utils.AppConstants.HTTP_STATUS;
import static wassalt.Utils.AppConstants.URL;

import java.util.ArrayList;

import org.apache.http.HttpStatus;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;

import wassalt.Services.HTTPService;
import wassalt.Utils.AppConstants;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author loai
 */
public class CreateAlarm extends CreateNotification {

	ProgressDialog dialog;
	final String NEWALARM_PATH = "/ManageNotifications/NewNotification_Generic";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// hide irrelevant data
		ArrayList<View> hide = new ArrayList<View>();
		hide.add(findViewById(R.id.AddContact));
		hide.add(findViewById(R.id.to));
		hide.add(findViewById(R.id.contact_error));
		hide.add(findViewById(R.id.ContactButton));
		hide.add(findViewById(R.id.favortite_contacts));
		try {
			View v = findViewById(R.id.checkbox_text);
			if (v != null)
				hide.add(findViewById(R.id.checkbox_text));
		} catch (Exception e) {
			// TODO: handle exception
		}
		for (int i = 0; i < hide.size(); i++) {
			hide.get(i).setVisibility(android.view.View.GONE);
			hide.get(i).setEnabled(false);
		}
		TextView header = (TextView) findViewById(R.id.title);
		header.setText(R.string.alarm_title);

		Button alarm_button = (Button) findViewById(R.id.done);
		alarm_button.setText(R.string.alarm_button);
	}

	public void addNotificationForTest() {
		adapter.addNotification();
	}

	@Override
	protected void insertDB(long userId, String phone, String place,
			boolean type, int when) {
		dialog = ProgressDialog.show(CreateAlarm.this, "",
				getString(R.string.request_load_msg), true, false);

		Intent offerServiceIntent = new Intent(this, HTTPService.class);
		offerServiceIntent.setAction(AppConstants.HTTP_INSERT_REQUEST_FILTER);
		offerServiceIntent.putExtra(URL, getString(R.string.server_url)
				+ NEWALARM_PATH);

		offerServiceIntent.putExtra("ntype", AppConstants.ALARM);
		offerServiceIntent.putExtra("idOwner", currentUserId);
		offerServiceIntent.putExtra("jsonString", getData().toString());
		startService(offerServiceIntent);
	}

	public boolean validateInput(View v) {

		TextView dest_err = (TextView) v.findViewById(R.id.address_error);
		TextView yalla_err = (TextView) v.findViewById(R.id.time_error);

		// make sure the address is inserted (done by dalia)
		// for user will only choose from map, cannot enter explicitly
		final EditText address = (EditText) v.findViewById(R.id.address);
		if (address.getText().length() == 0) {
			Toast.makeText(this, R.string.invalid_dest, Toast.LENGTH_SHORT)
					.show();
			dest_err.setText(R.string.invalid_dest);
			return false;
		} else
			dest_err.setText("");

		// make sure that the user entered a number in the edit text for the
		// when section
		// not complete
		final EditText wann = (EditText) v.findViewById(R.id.amount);
		if (wann.getText().length() == 0) {
			if (!type) {
				Toast.makeText(this, R.string.invalid_time, Toast.LENGTH_SHORT)
						.show();
				yalla_err.setText(R.string.invalid_time);
			} else {
				Toast.makeText(this, R.string.invalid_dist, Toast.LENGTH_SHORT)
						.show();
				yalla_err.setText(R.string.invalid_dist);
			}
			return false;
		} else {
			when = Integer.parseInt(wann.getText().toString());
			yalla_err.setText("");
		}
		return true;
	}

	private BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			int http_status = intent.getExtras().getInt(HTTP_STATUS);
			Log.e("Status", http_status + "");
			if (http_status == HttpStatus.SC_OK)
				Toast.makeText(getApplicationContext(), "Alarm created !!",
						Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(getApplicationContext(),
						"Error !Alarm not created !!", Toast.LENGTH_SHORT)
						.show();
			dialog.dismiss();
			Intent returnToActivity = new Intent();
			returnToActivity.setClass(getApplicationContext(),
					NotificationsListActivity.class);
			startActivity(returnToActivity);

		}
	};

	// Register the broadcast receiver with each onResume
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction(AppConstants.HTTP_INSERT_REQUEST_FILTER);
		registerReceiver(receiver, filter);

	}

	// UnRegister the broadcast receiver with each onPause
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}
}
